package init

import (
	"fmt"
	"mt-server/basic/config"

	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
)

func NaCosInit() {

	nacos := config.AppConfig.NaCos
	//create clientConfig
	clientConfig := constant.ClientConfig{
		NamespaceId:         nacos.Pid, //we can create multiple clients with different namespaceId to support multiple namespace.When namespace is public, fill in the blank string here.
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "/tmp/nacos/log",
		CacheDir:            "/tmp/nacos/cache",
		LogLevel:            "debug",
		Username:            nacos.UserName,
		Password:            nacos.Password,
	}

	// At least one ServerConfig
	serverConfigs := []constant.ServerConfig{
		{
			IpAddr:      nacos.Host,
			ContextPath: "/nacos",
			Port:        uint64(nacos.Port),
			Scheme:      "http",
		},
	}

	// Create config client for dynamic configuration
	configClient, err := clients.CreateConfigClient(map[string]interface{}{
		"serverConfigs": serverConfigs,
		"clientConfig":  clientConfig,
	})
	if err != nil {
		return
	}

	content, err := configClient.GetConfig(vo.ConfigParam{
		DataId: nacos.Did,
		Group:  nacos.Group})

	fmt.Println("NaCos配置成功", content)

}
